package org.exia.minisig.swing.view.navigation;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import org.exia.minisig.application.Utils;
import org.exia.minisig.model.object.DescriptibleElement;
import org.exia.minisig.model.object.Interest;
import org.exia.minisig.swing.view.home.HomeFram;

@SuppressWarnings("serial")
public class NavigationInterest extends JPanel {
	//
	private ItineraryNavigation parent;
	private ArrayList<Image> images;
	private DescriptibleElement element;
	private int navIndex;
	private String[] historic;
	//CONSTRUCTOR
	public NavigationInterest(ItineraryNavigation _parent, DescriptibleElement _element) {
		initializeVar(_parent, _element);
	}
	public void initializeVar(ItineraryNavigation _parent, DescriptibleElement _element){
		this.parent = _parent;
		historic = new String[5];
		this.setBackground(Color.white);
		this.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0, Color.LIGHT_GRAY));
		images = new ArrayList<>();
		this.element = _element;
		this.navIndex = 0;
		//-----------------------------//
		for (String pathImg: this.element.getImages()) {
			images.add(Utils.loadImage(pathImg));
		}
		//-----------------------------//
		if (element instanceof Interest){
			fillInterest();
		}else{
		fill();}
	}
	private void fillInterest() {
		this.removeAll();
		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;
		//PARAMETER
		gc.weightx=1;
		gc.weighty=1;
		//---------------------------------------//
		//BTN PREVIOUS
		gc.insets = new Insets(30, 10, 30, 10);
		gc.gridheight=1;
		gc.gridwidth= 1;
		gc.weightx = 0.3;
		gc.gridx=0;
		gc.gridy=0;
		JButton btnPrev = new JButton("<");
		btnPrev.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(navIndex > 0){
					navIndex--;
					fill();
				}
			}
		});
		this.add(btnPrev, gc);
		//IMG
		gc.insets = new Insets(0, 0, 0, 0);
		gc.gridheight=1;
		gc.gridwidth= 1;
		gc.weightx = 2;
		gc.gridx=1;
		gc.gridy=0;
		JLabel imgLabel =  new JLabel(" X ");
		imgLabel.setHorizontalAlignment(SwingConstants.CENTER);
		imgLabel.setVerticalAlignment(SwingConstants.CENTER);
		if(this.images.size() != 0){
			imgLabel = new JLabel(new ImageIcon(Utils.setIconSize(this.images.get(navIndex), 100, 100)));
		}
		this.add(imgLabel, gc);
		//BTN PREVIOUS
		gc.insets = new Insets(30, 10, 30, 10);
		gc.gridheight=1;
		gc.gridwidth= 1;
		gc.weightx = 0.3;
		gc.gridx=2;
		gc.gridy=0;
		JButton btnNext = new JButton(">");
		btnNext.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(navIndex < parent.getInterestOfItinerary().size() - 1){
					navIndex++;
					fill();
				}
			}
		});
		this.add(btnNext, gc);		
		//
		//BTN PREVIOUS
	
		
		
		this.revalidate();
	}
	//GET & SET
	public ArrayList<Image> getImages() {
		return images;
	}
	public void setUrlImages(ArrayList<Image> urlImages) {
		this.images = urlImages;
	}
	//METHODE
	public void fill(){
		this.removeAll();
		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;
		//PARAMETER
		gc.weightx=1;
		gc.weighty=1;
		//---------------------------------------//
		//BTN PREVIOUS
		gc.insets = new Insets(30, 10, 30, 10);
		gc.gridheight=1;
		gc.gridwidth= 1;
		gc.weightx = 0.3;
		gc.gridx=0;
		gc.gridy=0;
		JButton btnPrev = new JButton("<");
		btnPrev.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(navIndex > 0){
					navIndex--;
					fill();
				}
			}
		});
		this.add(btnPrev, gc);
		//IMG
		gc.insets = new Insets(0, 0, 0, 0);
		gc.gridheight=1;
		gc.gridwidth= 1;
		gc.weightx = 2;
		gc.gridx=1;
		gc.gridy=0;
		JLabel imgLabel =  new JLabel(" X ");
		imgLabel.setHorizontalAlignment(SwingConstants.CENTER);
		imgLabel.setVerticalAlignment(SwingConstants.CENTER);
		if(this.images.size() != 0){
			imgLabel = new JLabel(new ImageIcon(Utils.setIconSize(this.images.get(navIndex), 100, 100)));
		}
		this.add(imgLabel, gc);
		//BTN PREVIOUS
		gc.insets = new Insets(30, 10, 30, 10);
		gc.gridheight=1;
		gc.gridwidth= 1;
		gc.weightx = 0.3;
		gc.gridx=2;
		gc.gridy=0;
		JButton btnNext = new JButton(">");
//		
//		btnNext.addActionListener(new ActionListener() {
//			@Override
//			public void actionPerformed(ActionEvent arg0) {
//				if(navIndex < images.size() - 1){
//					int index = 0;
//					boolean notNull = false;
//					for (int i = 0; i < parent.getInterestOfItinerary().size(); i++) {
//						if (parent.getParent().getSelectedElement() != null){
//							notNull = true;
//							if (parent.getInterestOfItinerary().get(i) == (Interest) parent.getParent().getSelectedElement()){
//								index =  i;
//							}
//						}
//					}
//					if (notNull && index +1  < parent.getInterestOfItinerary().size()){
//						parent.getParent().setSelectedElement(
//								parent.getInterestOfItinerary().get(index +1)
//								);
//						for (Interest x : parent.getInterestOfItinerary()) {
//							if (x.getLabel() == parent.getInterestOfItinerary().get(index +1).getLabel()){
//								parent.getParent().getMap().setSelectedElement(x);
//							}
//						}
//						
//					}
//					
//					fill();
//				}
//			}
//		});
//		
		this.add(btnNext, gc);		
		//
		this.revalidate();
	}
}
